package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyBaoDing;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.Map;

/**
 * 安心分期-保定房金二选70api 信息流
 * @ Author 江小游
 * @ Date 2025/6/16 16:33
 */
@ApiPlan(productId = "248", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskBaoDingQuDao implements ApiOrgPhoneMaskConvertHandler {
    private static final String PUBLIC_KEY = "hbt4j8ql7n7yjtfb";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataChangeMessage) throws Exception {
        // 创建实体类
        Map<String, Object> map = MapUtils.newHashMap(newMap -> newMap.put("phone", MD5.create().digestToBase64Str(apiDataChangeMessage.getPhoneMask())));
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        String data = jsonObject.getString("data");
        if (data != null && data.equals(" ")) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        return RespOrgPhoneMaskDataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyMessage) throws Exception {
        // 创建进件实体类
        OrgApplyBaoDing orgApplyBaoDing = new OrgApplyBaoDing();
        // 创建拓展工具类
        com.ts.api.module.api.convert.core.utils.ApiFieldConverter apiFieldConverter = com.ts.api.module.api.convert.core.utils.ApiFieldConverter.create(apiApplyMessage);
        // 客户姓名
        orgApplyBaoDing.setUsername(apiApplyMessage.getUserName());
        // 手机号
        orgApplyBaoDing.setPhone(apiApplyMessage.getPhone());
        // 城市（上海、北京……） 不带市
        orgApplyBaoDing.setCity(apiApplyMessage.getCity().replace("市", ""));
        // 需求金额（元）
        orgApplyBaoDing.setLoan(apiFieldConverter.getLoanAmountInt());
        // 放款金额
        orgApplyBaoDing.setPrice(apiFieldConverter.getLoanAmountInt());
        // 是否有公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyMessage.getProvidentFund(), "1", 2)
                .when(apiApplyMessage.getProvidentFund(), "2", 1)
                .when(apiApplyMessage.getProvidentFund(), "3", 1)
                .when(apiApplyMessage.getProvidentFund(), "4", 1)
                .end(2);
        orgApplyBaoDing.setGjj(providentFund);
        //  工资代发 2.无 1.有
        if (ValidParam.isNotEmpty(apiApplyMessage.getSalaryPayMethod())) {
            Integer salary = ChooseEq.create()
                    .when(apiApplyMessage.getSalaryPayMethod(), "1", 1)
                    .when(apiApplyMessage.getSalaryPayMethod(), "2", 2)
                    .when(apiApplyMessage.getSalaryPayMethod(), "3", 2)
                    .end(2);
            orgApplyBaoDing.setIspayroll(salary);
        }
        // 是否有房
        Integer realEstateInfo = ChooseEq.create()
                .when(apiApplyMessage.getRealEstateInfo(), "1", 2)
                .when(apiApplyMessage.getRealEstateInfo(), "2", 1)
                .when(apiApplyMessage.getRealEstateInfo(), "3", 1)
                .when(apiApplyMessage.getRealEstateInfo(), "4", 1)
                .when(apiApplyMessage.getRealEstateInfo(), "5", 1)
                .when(apiApplyMessage.getRealEstateInfo(), "6", 1)
                .end();
        orgApplyBaoDing.setHouse(realEstateInfo);
        // 是否有车
        Integer carInfo = ChooseEq.create()
                .when(apiApplyMessage.getCarInfo(), "1", 2)
                .when(apiApplyMessage.getCarInfo(), "2", 1)
                .when(apiApplyMessage.getCarInfo(), "3", 1)
                .when(apiApplyMessage.getCarInfo(), "4", 1)
                .end();
        orgApplyBaoDing.setCar(carInfo);
        // 是否有表单
        Integer insurancePolicy = ChooseEq.create()
                .when(apiApplyMessage.getInsurancePolicy(), "1", 2)
                .when(apiApplyMessage.getInsurancePolicy(), "2", 1)
                .when(apiApplyMessage.getInsurancePolicy(), "3", 1)
                .when(apiApplyMessage.getInsurancePolicy(), "4", 1)
                .end(2);
        orgApplyBaoDing.setInsurance(insurancePolicy);
        // 信用卡
        orgApplyBaoDing.setXyk(apiFieldConverter.hasCreditLimitToNumber());
        // 是否有社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyMessage.getSocialSecurity(), "1", 2)
                .when(apiApplyMessage.getSocialSecurity(), "2", 1)
                .when(apiApplyMessage.getSocialSecurity(), "3", 1)
                .when(apiApplyMessage.getSocialSecurity(), "4", 1)
                .end(2);
        orgApplyBaoDing.setShebao(socialSecurity);
        orgApplyBaoDing.setPay_tax(2);
        orgApplyBaoDing.setParticles(2);
        // 是否有营业执照
        Integer bizLicenseYearToStr = ChooseEq.create()
                .when(apiApplyMessage.getBizLicenseYear(), "1", 2)
                .when(apiApplyMessage.getBizLicenseYear(), "2", 1)
                .when(apiApplyMessage.getBizLicenseYear(), "3", 1)
                .when(apiApplyMessage.getBizLicenseYear(), "4", 1)
                .when(apiApplyMessage.getBizLicenseYear(), "5", 1)
                .end(2);
        orgApplyBaoDing.setBusiness_license(bizLicenseYearToStr);
        // 年龄
        orgApplyBaoDing.setAge(apiFieldConverter.getAgeInt());


        // 是否有备注
        StringBuilder stringBuilder = new StringBuilder();
        if (ValidParam.isNotEmpty(apiApplyMessage.getCarInfo())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getCarInfo(), "1", "无车产")
                    .when(apiApplyMessage.getCarInfo(), "2", "有车不抵押")
                    .when(apiApplyMessage.getCarInfo(), "3", "有车可抵押")
                    .when(apiApplyMessage.getCarInfo(), "4", "有车")
                    .end();
            stringBuilder.append("车辆情况：").append(text).append(";").append("\n");
        }
        //  房产情况
        if (ValidParam.isNotEmpty(apiApplyMessage.getRealEstateInfo())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getRealEstateInfo(), "1", "无房产")
                    .when(apiApplyMessage.getRealEstateInfo(), "2", "有房不抵押")
                    .when(apiApplyMessage.getRealEstateInfo(), "3", "有房可抵押")
                    .when(apiApplyMessage.getRealEstateInfo(), "4", "有房产")
                    .when(apiApplyMessage.getRealEstateInfo(), "5", "有按揭房产")
                    .when(apiApplyMessage.getRealEstateInfo(), "6", "有全款房产")
                    .end();
            stringBuilder.append("房产情况：").append(text).append(";").append("\n");
        }
        //  公积金
        if (ValidParam.isNotEmpty(apiApplyMessage.getProvidentFund())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getProvidentFund(), "1", "无公积金")
                    .when(apiApplyMessage.getProvidentFund(), "2", "缴纳6个月以下")
                    .when(apiApplyMessage.getProvidentFund(), "3", "缴纳6个月以上")
                    .when(apiApplyMessage.getProvidentFund(), "4", "有公积金")
                    .end("无公积金");
            stringBuilder.append("公积金：").append(text).append(";").append("\n");
        }
        //   保险保单
        if (ValidParam.isNotEmpty(apiApplyMessage.getInsurancePolicy())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getInsurancePolicy(), "1", "无保险保单")
                    .when(apiApplyMessage.getInsurancePolicy(), "2", "缴纳未满1年")
                    .when(apiApplyMessage.getInsurancePolicy(), "3", "缴纳1年以上")
                    .when(apiApplyMessage.getInsurancePolicy(), "4", "有保险保单")
                    .end("无保险保单");
            stringBuilder.append("保险保单：").append(text).append(";").append("\n");
        }
        // 社保缴纳
        if (ValidParam.isNotEmpty(apiApplyMessage.getSocialSecurity())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getSocialSecurity(), "1", "无社保")
                    .when(apiApplyMessage.getSocialSecurity(), "2", "缴纳6个月以下")
                    .when(apiApplyMessage.getSocialSecurity(), "3", "缴纳6个月以上")
                    .when(apiApplyMessage.getSocialSecurity(), "4", "有社保")
                    .end("无社保");
            stringBuilder.append("社保缴纳：").append(text).append(";").append("\n");
        }
        // 营业执照注册年限
        if (ValidParam.isNotEmpty(apiApplyMessage.getBizLicenseYear())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getBizLicenseYear(), "1", "未注册")
                    .when(apiApplyMessage.getBizLicenseYear(), "2", "注册1年以下")
                    .when(apiApplyMessage.getBizLicenseYear(), "3", "注册1~5年")
                    .when(apiApplyMessage.getBizLicenseYear(), "4", "注册5年以上")
                    .end();
            stringBuilder.append("营业执照：").append(text).append(";").append("\n");

        }
        orgApplyBaoDing.setContent(stringBuilder.toString());
        AES aes = AES.createEcb(PUBLIC_KEY.getBytes());
        String data = aes.encryptToBase64Str(JsonUtils.toJson(orgApplyBaoDing));
        Map<String, Object> map = MapUtils.newHashMap(newMap -> newMap.put("data", data));
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
